package com.unidt.www.module.bpm.dal.mysql.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.unidt.www.framework.mybatis.core.mapper.BaseMapperX;
import com.unidt.www.module.bpm.dal.dataobject.task.BpmTaskExtDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Mapper
public interface BpmTaskExtMapper extends BaseMapperX<BpmTaskExtDO> {

    default void updateByTaskId(BpmTaskExtDO entity) {
        update(entity, new LambdaQueryWrapper<BpmTaskExtDO>().eq(BpmTaskExtDO::getTaskId, entity.getTaskId()));
    }

    default List<BpmTaskExtDO> selectListByTaskIds(Collection<String> taskIds) {
        return selectList(BpmTaskExtDO::getTaskId, taskIds);
    }

    default BpmTaskExtDO selectByTaskId(String taskId) {
        return selectOne(BpmTaskExtDO::getTaskId, taskId);
    }

    default Map<String, BpmTaskExtDO> getByProcessInstanceIdAndResult(String processInstanceId, Integer result) {
        LambdaQueryWrapper<BpmTaskExtDO> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(BpmTaskExtDO::getProcessInstanceId, processInstanceId);
        lambdaQueryWrapper.eq(BpmTaskExtDO::getResult, result);
        lambdaQueryWrapper.orderByDesc(BpmTaskExtDO::getCreateTime);

        List<BpmTaskExtDO> list = selectList(lambdaQueryWrapper);
        Map<String, BpmTaskExtDO> map = new HashMap<>();
        if (!list.isEmpty()) {
            map = list.stream().collect(Collectors.toMap(BpmTaskExtDO::getTaskId, a -> a, (k1, k2) -> k1));
        }
        return map;

    }

    ;
}
